package com.study.jdbc.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import com.study.jdbc.domain.Customer;
import com.study.jdbc.factory.BeanFactory;
import com.study.jdbc.service.CustomerService;
import com.study.jdbc.utils.PageBean;
import com.study.jdbc.utils.WebUtil;
import com.study.jdbc.vo.CustomerFormBean;

public class CustomerController extends HttpServlet {
	
	private static final long serialVersionUID = -3572476193803389573L;
	
	private CustomerService customerService = BeanFactory.getInstance().getCustomerServiceImpl() ;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8") ;
		response.setContentType("text/html;charset=UTF-8") ;
		String operate = request.getParameter("operate") ;
		//查询所有客户记录
		if("showAllCustomers".equals(operate)) {
			showAllCustomers(request, response) ;
		}
		//添加客户记录
		if("addCustomer".equals(operate)) {
			addCustomer(request, response) ;
		}
		//删除一个客户记录
		if("deleteOneCustomer".equals(operate)) {
			deleteOneCustomer(request, response) ;
		}
		//删除多个客户记录
		if("deleteMultiCustomers".equals(operate)) {
			deleteMultiCustomers(request, response) ;
		}
		//获取待修改的客户信息
		if("editCustomerUI".equals(operate)) {
			editCustomerUI(request, response) ;
		}
		//修改客户信息
		if("editCustomer".equals(operate)) {
			editCustomer(request, response) ;
		}
	}

	/**
	* @Name: showAllCustomers
	* @Description: 查询所有客户信息
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-16 下午9:30:03
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Return: void
	 */
	private void showAllCustomers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//查询所有客户信息，不分页实现
//		List<Customer> customers = customerService.findAllCustomers() ;
//		request.setAttribute("customers", customers) ;
		//查询所有客户信息，分页实现
		String currentPageNum = request.getParameter("currentPageNum") ;
		PageBean<Customer> pageBean = customerService.findCustomerPageRecords(currentPageNum) ;
		pageBean.setUrl("/servlet/CustomerController?operate=showAllCustomers") ;
		request.setAttribute("pageBean", pageBean) ;
		request.getRequestDispatcher("/listCustomer.jsp").forward(request, response) ;
	}
	
	/**
	* @Name: addCustomer
	* @Description: 添加客户记录
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-16 下午10:27:00
	* @Parameters: @param request
	* @Parameters: @param response
	* @Return: void
	 */
	private void addCustomer(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			//将页面中的数据封装到FormBean中
			CustomerFormBean formBean = WebUtil.fillBean(request, CustomerFormBean.class) ;
			//将VO对象转换成DO对象
			Customer customer = new Customer() ;
			//处理日期格式的字段
			ConvertUtils.register(new DateLocaleConverter(), Date.class) ;
			//处理字符串格式的字段
			BeanUtils.copyProperties(customer, formBean) ;
			//单独处理爱好数组
			String[] hobbies = formBean.getHobbies() ;
			if(hobbies != null && hobbies.length > 0) {
				StringBuffer buffer = new StringBuffer() ;
				for (int i = 0; i < hobbies.length; i++) {
					buffer.append(hobbies[i]) ;
					if(i < hobbies.length -1) {
						buffer.append(",") ;
					}
				}
				customer.setPreference(buffer.toString()) ;
			}
			customerService.saveCustomer(customer) ;
			//redirect after post : 提交后重定向到前一个跳转页面
			response.sendRedirect(request.getContextPath()) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response) ;
	}
	
	/**
	* @throws ServletException 
	* @Name: deleteCustomer
	* @Description: 删除客户记录
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-17 下午1:49:25
	* @Parameters: @param request
	* @Parameters: @param response
	* @Return: void
	 */
	private void deleteOneCustomer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String customerId = request.getParameter("customerId") ;
		customerService.deleteCustomerById(customerId) ;
		response.sendRedirect(request.getContextPath()) ;
	}
	
	/**
	* @Name: deleteMultiCustomers
	* @Description: 删除多个客户记录
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-17 下午2:43:29
	* @Parameters: @param request
	* @Parameters: @param response
	* @Return: void
	 */
	private void deleteMultiCustomers(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids") ;
		String[] idArray = ids.split(",") ;
		for (int i = 0; i < idArray.length; i++) {
			String customerId = idArray[i] ;
			customerService.deleteCustomerById(customerId) ;
		}
		response.sendRedirect(request.getContextPath()) ;
	}
	
	/**
	* @throws ServletException 
	* @Name: editCustomerUI
	* @Description: 获取待修改的客户信息
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-17 下午2:58:00
	* @Parameters: @param request
	* @Parameters: @param response
	* @Return: void
	 */
	private void editCustomerUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String customerId = request.getParameter("customerId") ;
		Customer customer = customerService.findCustomerById(customerId) ;
		request.setAttribute("customer", customer) ;
		request.getRequestDispatcher("/editCustomer.jsp").forward(request, response) ;
	}
	
	/**
	* @Name: editCustomer
	* @Description: 修改客户信息
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-17 下午3:26:39
	* @Parameters: @param request
	* @Parameters: @param response
	* @Return: void
	 */
	private void editCustomer(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			//将页面中的数据封装到FormBean中
			CustomerFormBean formBean = WebUtil.fillBean(request, CustomerFormBean.class) ;
			//将VO对象转换成DO对象
			Customer customer = new Customer() ;
			//处理日期格式的字段
			ConvertUtils.register(new DateLocaleConverter(), Date.class) ;
			//处理字符串格式的字段
			BeanUtils.copyProperties(customer, formBean) ;
			//单独处理爱好数组
			String[] hobbies = formBean.getHobbies() ;
			if(hobbies != null && hobbies.length > 0) {
				StringBuffer buffer = new StringBuffer() ;
				for (int i = 0; i < hobbies.length; i++) {
					buffer.append(hobbies[i]) ;
					if(i < hobbies.length -1) {
						buffer.append(",") ;
					}
				}
				customer.setPreference(buffer.toString()) ;
			}
			customerService.updateCustomer(customer) ;
			response.sendRedirect(request.getContextPath()) ;
		} catch (Exception e) {
			e.printStackTrace(); 
		}
	}



}
